<template>
  <div>
    <h1>列表</h1>
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input
          v-model="searchForm.name"
          placeholder="请输出关键字"
          clearable
        />

        <el-date-picker
          v-model="searchForm.beginTime"
          type="datetime"
          placeholder="Select date and time"
        />

        <el-date-picker
          v-model="searchForm.endTime"
          type="datetime"
          placeholder="Select date and time"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="queryList">查询</el-button>
        <el-button
          type="danger"
          @click="onDeleteAll"
          :disabled="multipleSelection.length == 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="id" label="orderId" />
      <el-table-column prop="name" label="订单名" />
      <el-table-column prop="createtime" label="createtime" />
      <el-table-column prop="orderStatus" label="orderStatus">
        <template #default="scope">
          <el-tag v-if="scope.row.orderStatus === 0">待支付</el-tag>
          <el-tag type="success" v-if="scope.row.orderStatus === 1"
            >支付成功</el-tag
          >
          <el-tag type="danger" v-if="scope.row.orderStatus === 2"
            >取消订单</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="content" label="content" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            v-if="scope.row.orderStatus === 0"
            @click="pay(scope.row)"
            >支付</el-button
          >
          <el-button type="danger" @click="onDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 30px">
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog
        v-model="dialogFormVisible"
        title="Shipping address"
        width="500"
      >
        <el-form :model="form">
          <el-form-item label="订单编号" :label-width="formLabelWidth">
            <el-input v-model="form.orderCode" autocomplete="off" disabled />
          </el-form-item>

          <el-form-item label="下单时间" :label-width="formLabelWidth">
            <el-input v-model="form.createtime" autocomplete="off" disabled />
          </el-form-item>

          <el-form-item label="支付状态" :label-width="formLabelWidth">
            <el-radio-group v-model="form.orderStatus">
              <el-radio value="1">支付</el-radio>
              <el-radio value="2">取消订单</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="取消原因" :label-width="formLabelWidth">
            <el-input
              type="textarea"
              v-model="form.content"
              autocomplete="off"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="updateOrderStatus()">
              确认
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getOrderList, updateOrder, deleteById, deleteAll } from "../api/api";

import {
  ElMessage,
  ElMessageBox,
  type FormInstance,
  type FormRules,
} from "element-plus";
const dialogFormVisible = ref(false);
const tableData = ref([]);
const total = ref(0);
const formLabelWidth = "140px";
const multipleSelection = ref<any[]>([]);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
  beginTime: null,
  endTime: null,
  name: null,
});
const form = ref({
  orderId: null,
  orderCode: null,
  createtime: null,
  orderStatus: null,
  content: null,
});
const loadList = () => {
  getOrderList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};
const pay = (row: any) => {
  console.log(row);
  dialogFormVisible.value = true;
  //把数据进行回县
  // form.value = row;    这是一种浅拷贝，直接赋值
  //assign 转换一个新的对象
  Object.assign(form.value, row);
};

const updateOrderStatus = () => {
  updateOrder(form.value).then((res) => {
    if (res.data.code == 200) {
      //修改成功
      ElMessage.success("修改成功！");
      //关闭弹框
      dialogFormVisible.value = false;
      //页面刷新
      loadList();
    } else {
      //修改失败
      ElMessage.error(res.data.msg);
    }
  });
};
//单删
const onDelete = (row: any) => {
  console.log(row);
  ElMessageBox.confirm("删除后数据不可恢复，是否继续?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteById(row.id).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};
//全删
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log(multipleSelection.value);
};
const onDeleteAll = () => {
  deleteAll(multipleSelection.value).then((res) => {
    ElMessageBox.confirm("删除后数据不可恢复，是否继续?", "警告", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      })
      .catch(() => {});
  });
};

const queryList = () => {
  loadList();
};

onMounted(() => {
  loadList();
});
</script>
